package com.yc.moive.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.yc.moive.bean.Moives;
import com.yc.moive.bean.MoivesType;

@WebServlet("/up/*")
public class UpMovieHttp extends IBaseHttp{
	private SqlSessionFactory factory =null;
	private SqlSession session = null;
	private List<Moives> list =null;
	
	{
		try(InputStream reader = Resources.getResourceAsStream("mybatis-config.xml")){//Reader reader = Resources.getResourceAsReader("mybatis-config.xml")
			factory = new SqlSessionFactoryBuilder().build(reader);
			session = factory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void findType(HttpServletRequest request, HttpServletResponse response) throws IOException{
		List<MoivesType> list=session.selectList("up.findsType");
		if(list != null || !list.isEmpty()) {
			this.send(response, 200,list);
			return;
		}
		this.send(response, 500,"没有发现类别");
	}
	
	public void uptext(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String mname = request.getParameter("mname");
		String mart = request.getParameter("mart");
		String mguide = request.getParameter("mguide");
		String mprice = request.getParameter("mprice");
		String typeInfo = request.getParameter("typeInfo");
		
		MoivesType list = session.selectOne("up.findsTid", typeInfo);
		int tid = list.getTid();
		Integer price = null;
		try {
			 price= Integer.parseInt(mprice);
		} catch (Exception e) {
				this.send(response,404,"出错,价格请输入数字");
				return;
		}
		Moives moives = new Moives();
		moives.setMart(mart);
		moives.setMguide(mguide);
		moives.setTid(tid);
		moives.setMname(mname);
		moives.setMprice(price);
		int result = session.insert("up.moive",moives);
		session.commit();
		if(result ==1) {
			this.send(response, 200,"发布成功");
		}else {
			this.send(response, 500,"发布失败");
		}
		
	}
	
	public void selectMovic(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String mname = request.getParameter("mname");
		String mart = request.getParameter("mart");
		String mguide = request.getParameter("mguide");
		String mprice1 = request.getParameter("mprice1");
		String mprice2 = request.getParameter("mprice2");
		String typeInfo = request.getParameter("typeInfo");
		
		Integer tid;
		if("全部".trim().equals(typeInfo.trim())) {
			tid=null;
		}else {
			MoivesType list = session.selectOne("up.findsTid", typeInfo);
			 tid= list.getTid();
		}
		
		Integer price1 = null;
		Integer price2 = null;
		
		try {
			if(mprice1 != null && !"".equals(mprice1) && !mprice1.equals("null")) {
				price1 = new Integer(mprice1);
			}
			if(mprice2 != null && !"".equals(mprice2) &&!mprice1.equals("null")) {
				price2 = new Integer(mprice2);
			}
		} catch (NumberFormatException e) {
			this.send(response, 500,"价格应该填数字");
			return;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("mname",mname);
		map.put("mart",mart);
		map.put("mguide",mguide);
		map.put("mprice1",price1);
		map.put("mprice2",price2);
		map.put("tid",tid);
		System.out.println(tid);
		List<Moives> list = session.selectList("up.findsMoive", map);
		System.out.println(list.size());
		System.out.println(list);
		if(list.size()<=0) {
			this.send(response, 404,"没有找到值");
		}else {
			this.send(response, 200, list);
		}
	}
}
